package sword_to_1;

public class LeeCode_47 {
    public static void main(String[] args) {
        System.out.println(maxValue(new int[][]{
                {1, 3, 1},
                {1, 5, 1},
                {4, 2, 1}}));
    }
    private static int maxValue(int[][] grid) {
        int m = grid.length;
        int n = grid[0].length;
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                if (i == 0 && j == 0){
                    continue;
                }
                if (j == 0){
                    grid[i][j] += grid[i - 1][j];
                }else if (i == 0){
                    grid[i][j] += grid[i][j - 1];
                }else {
                    grid[i][j] += Math.max(grid[i - 1][j], grid[i][j - 1]);
                }
            }
        }
        return grid[m - 1][n - 1];
    }
}
